package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.MainActivity;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.UserBeanResult;
import com.paobuji.aidebao.myapplication.tool.ACache;
import com.paobuji.aidebao.myapplication.tool.MyIntentTools;
import com.paobuji.aidebao.myapplication.tool.ProgressDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.goRegist)
    TextView goRegist;
    @BindView(R.id.loginBtn)
    Button loginBtn;
    @BindView(R.id.accountText)
    EditText accountText;
    @BindView(R.id.passText)
    EditText passText;
    Context context;
    ACache aCache;
    MyIntentTools myIntentTools;
    UserBeanResult.DataBean.UserBean userBean;
    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);
        myIntentTools = new MyIntentTools();
        progressDialog = new ProgressDialog(this);
        context = getApplicationContext();
        aCache = ACache.get(context);
        userBean = new UserBeanResult.DataBean.UserBean();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @OnClick({R.id.goRegist, R.id.loginBtn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goRegist:
                //前往注册界面
                goRegistActivity();
                break;
            case R.id.loginBtn:
                //前往登录界面
                getLogin();
                break;
        }
    }

    /**
     * 请求网络通过电话号码登录
     */
    public void getLogin(){
        //如果账号密码都不为空
        if(chackEmpty(accountText) && chackEmpty(passText)){
            progressDialog.showDialog();
            myIntentTools.loginUserByPhone(context,accountText.getText().toString().trim(),passText.getText().toString().trim());
        }
        myIntentTools.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
            @Override
            public void getObjectDate(Object data) {

                 if(((UserBeanResult)data).getCode() == 0){
                     userBean = ((UserBeanResult)data).getData().getUser();
                     aCache.put("userBean", userBean);

                     //前往首界面
                     goMainActivity();

                     //表示用户已经登录
                     aCache.put("userstate","true");

                 }else if(((UserBeanResult)data).getCode() == 1){
                     progressDialog.cancelDialog();
                     toastLog(R.string.erroneous + "");
                 }else if(((UserBeanResult)data).getCode() == 2){
                     progressDialog.cancelDialog();
                     toastLog(R.string.exist + "");
                 }else{
                     progressDialog.cancelDialog();
                     toastLog(R.string.erroneous + "");
                 }
            }
        });
    }

    /**
     * 为空监测
     * @param e
     * @return
     */
    public boolean chackEmpty(EditText e){
        if("".equals(e.getText().toString().trim()) || e.getText().toString().trim() == null){
            toastLog(R.string.erroneous + "");
            return false;
        }
        return true;
    }


    /**
     * 跳转到首界面
     */
    public void goMainActivity(){
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
    }

    /**
     * 跳转到注册界面
     */
    public void goRegistActivity(){
        Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
        startActivity(intent);
    }

}
